#include "dxdiagcom.h"
#include "wine/debug.h"
#include "shlwapi.h"
#include <string.h>

WINE_DEFAULT_DEBUG_CHANNEL(dxdiag);

XML_Node* XML_initializeNode(LPCWSTR tag, LPCWSTR content)
{
    WCHAR* tch;
    XML_Node *node = malloc(sizeof(XML_Node));
    node->tag = (LPWSTR)calloc(lstrlenW(tag)+1,sizeof(WCHAR));
    wcscpy(node->tag,tag);
    if (content)
    {
        node->content = (LPWSTR)calloc(lstrlenW(content)+1,sizeof(WCHAR));
        wcscpy(node->content,content);
        /* Replace angle brackets with something nice */
        tch = StrChrW(node->content,'<');
        while (tch != NULL)
        {
            *tch = '[';
            tch = StrChrW(tch+1,'<');
        }
        tch = StrChrW(node->content,'>');
        while (tch != NULL)
        {
            *tch = ']';
            tch = StrChrW(tch+1,'>');
        }
    }
    else
    node->content = NULL;
    node->child = NULL;
    node->sibling = NULL;

    return node;
}

XML_Node* XML_insertChildNode(XML_Node* node, LPCWSTR tag, LPCWSTR content)
{
    if (node->child)
        return XML_appendSiblingNode(node->child,tag,content);
    else
    {
        node->child = XML_initializeNode(tag,content);
        return node->child;
    }

}


XML_Node* XML_appendSiblingNode(XML_Node* node, LPCWSTR tag, LPCWSTR content)
{
    if (node->sibling)
         return XML_appendSiblingNode(node->sibling,tag,content);
    else
    {
        node->sibling = XML_initializeNode(tag,content);
        return node->sibling;
    }
}

void XML_freeTree(XML_Node* node)
{
    if (node->tag)
        free(node->tag);
    if (node->content)
        free(node->content);
    if (node->child)
        XML_freeTree(node->child);
    if (node->sibling)
        XML_freeTree(node->sibling);
    free(node);
}

void XML_parseTree(XML_Node* root, FILE* filp)
{
    /* XXX fixme
    _ftprintf(filp,_T("<%s>"),root->tag);
    if (root->content)
        _ftprintf(filp,_T("%s"),root->content);
    else if (root->child)
        _ftprintf(filp,_T("\n"));
    if (root->child)
        XML_parseTree(root->child,filp);
        _ftprintf(filp,_T("</%s>\n"),root->tag);
    if (root->sibling)
        XML_parseTree(root->sibling,filp);
    */
}
